package com.gljx.web.controller;

import com.gljx.util.*;
import com.gljx.web.Entity.*;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author lcx
 * @Date 2017/11/3 0003 下午 1:40
 * 基础资料->财务基础资料->协议驾校
 */
@Controller
@RequestMapping("/")
public class DrivingController {

    @Autowired
    private DrivingService drivingService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CustomLogService customLogService;

    /*协议驾校的子表车辆业务*/
    @Autowired
    private DrivingentrysService drivingentrysService;

    /**
     * @Author lcx
     * @Date 2017/11/3 0003 下午 1:55
     * 查询协议驾校
     */
    @RequestMapping(value = "/select_driving/look.action")
    public String selDriving(HttpServletRequest request, String name
            , @RequestParam(value = "alertType", defaultValue = "") String alertType) {

        /*获取session中的登录信息*/
        User user = (User) request.getSession().getAttribute("user");
        /*查询收费项目*/
        name = name == null ? "" : name.trim();
        List<Driving> drivingList = drivingService.selectDriving(user.getFcompanyid(), name);

        request.setAttribute("drivingList", drivingList);
        request.setAttribute("name", name);
        request.setAttribute("alertType", alertType);

        return "backHome/basicData/financeData/driving/drivingList";
    }

    /**
     * @Author lcx
     * @Date 2017/11/1 11:41
     * 跳转添加协议驾校窗口
     */
    @RequestMapping(value = "/go_add_driving")
    public String goAddDriving(HttpServletRequest request, String alertType) throws Exception {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //获取部门
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        //获取上一个编码
        /*String number = customLogService.selectFunmber("CT_DS_DRIVING",user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("XYJX");

        request.setAttribute("fnamel2", department.getFnameL2());
        request.setAttribute("fnumber", fnumber);
        request.setAttribute("alertType", alertType);


        return "backHome/basicData/financeData/driving/drivingAdd";
    }

    /**
     * @Author lcx
     * @Date 2017/11/1 13:57
     * 添加收费
     */
    @RequestMapping(value = "add_driving")
    public String addDriving(HttpServletRequest request,
                             String fnumber, String fnameL2, String fsimplename,
                             String fdescriptionL2, String cfchepiao[],
                             String cfchexing[], String cfdesc[]) throws Exception {
        Driving driving = new Driving();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();

        /*系统生成的属性*/
        driving.setFid(uuid);
        driving.setCfficompanyid(user.getFcompanyid());
        driving.setFcreatorid(user.getFid());
        driving.setFcreatetime(createTime);
        driving.setFcontrolunitid(user.getFcompanyid());
        driving.setCreateName(user.getName());

        /*从界面获取的属性*/
        /*编码*/
        driving.setFnumber(fnumber);
        /*名称*/
        driving.setFnameL2(fnameL2);
        /*简称*/
        driving.setFsimplename(fsimplename);
        /*描述*/
        driving.setFdescriptionL2(fdescriptionL2);

        drivingService.addDriving(driving);

        /*子表车辆实体*/
        Drivingentrys drivingentrys = new Drivingentrys();

        //TODO 前端js判断数据是否发生改变 如果没改变则不用传这条记录
        for (int i = 0; i < cfchepiao.length; i++) {
            /*自动生成uuid*/
            drivingentrys.setFid(GetFid.UUIDIntoFid());
            /*对应主表的fid*/
            drivingentrys.setFparentid(uuid);
            /*名称*/
            drivingentrys.setCfchepiao(cfchepiao[i]);
            /*简称*/
            drivingentrys.setCfchexing(cfchexing[i]);
            /*描述*/
            try {
                drivingentrys.setCfdesc(cfdesc[i]);
            } catch (ArrayIndexOutOfBoundsException e) {
                drivingentrys.setCfdesc("");
            }
            drivingentrysService.addDrivingentrys(drivingentrys);
        }

        customLogService.addLog("财务基础资料", createTime + "对协议驾校进行操作", null, user.getFid());
        return "redirect:/DrivingSys/select_driving/look.action?alertType=bc";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:33
     * 跳转到协议驾校修改页面
     */
    @RequestMapping(value = "go_update_driving")
    public String goUpdateDriving(HttpServletRequest request, String fid, String xqid) throws Exception {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //把查询到的数据传给页面
        Driving driving = drivingService.queryDrivingByFid(fid);
        //获取部门
        List<Drivingentrys> drivingentrysList = drivingentrysService.queryDrivingentrysByFid(fid);

        /*如果页面传入xqid则进查找操作*/
        if (xqid != null) {
            request.setAttribute("xqid", xqid);
            driving = drivingService.queryDrivingByFid(xqid);
            drivingentrysList = drivingentrysService.queryDrivingentrysByFid(xqid);
        }

        /*用控制单元id去查询部门*/
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        request.setAttribute("drivingentrysList", drivingentrysList);
        request.setAttribute("department", department.getFnameL2());
        request.setAttribute("driving", driving);
        request.setAttribute("dif", fid);

        return "backHome/basicData/financeData/driving/updateDriving";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:31
     * 对财务基础资料修改
     */
    @RequestMapping(value = "update_driving")
    public String updateDriving(HttpServletRequest request, String fseq[], String updateid[], String fid, String did[],
                                String fnumber, String fnameL2, String fsimplename,
                                String fdescriptionL2, String cfchepiao[],
                                String cfchexing[], String cfdesc[]) throws Exception {


        Driving driving = new Driving();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //公司id
        driving.setCfficompanyid(user.getFcompanyid());
        //控制单元id
        driving.setFcontrolunitid(user.getFcompanyid());
        //获取当前修改时间
        String updateTime = TimeFormat.timeFormat(new Date());
        //修改时间
        driving.setFlastupdatetime(updateTime);
        //修改人id
        driving.setFlastupdateuserid(user.getFid());

        /*从页面获取的fid*/
        driving.setFid(fid);
        driving.setFnumber(fnumber);
        driving.setFnameL2(fnameL2);
        driving.setFsimplename(fsimplename);
        driving.setFdescriptionL2(fdescriptionL2);

        drivingService.modifyDrivingByFid(driving);

        /*子类*/
        Drivingentrys drivingentrys = new Drivingentrys();

        for (int i = 0; i < cfchepiao.length; i++) {
            try {
                drivingentrys.setFid(did[i]);
            } catch (ArrayIndexOutOfBoundsException e) {
                //如果抛出为空异常视为添加来一条数据
                //如果传入的did数组个数过少抛出异常则进行插入操作
                drivingentrys.setFid(GetFid.UUIDIntoFid());
                drivingentrys.setCfchepiao(cfchepiao[i]);
                drivingentrys.setCfchexing(cfchexing[i]);
                drivingentrys.setCfdesc(cfdesc[i]);
                drivingentrys.setFparentid(fid);
                drivingentrysService.addDrivingentrys(drivingentrys);
            }
            drivingentrys.setFseq(Long.valueOf(fseq[i]));
            drivingentrys.setCfchepiao(cfchepiao[i]);
            drivingentrys.setCfchexing(cfchexing[i]);
            try {
                drivingentrys.setCfdesc(cfdesc[i]);
            } catch (ArrayIndexOutOfBoundsException e) {
                drivingentrys.setCfdesc("");
            }

            drivingentrysService.modifyDrivingentrys(drivingentrys);

            //根据前端记录的删除id来删除需要删除的数据
            for (String id : updateid) {
                drivingentrysService.deleteDrivingentrys(id);
            }
        }
        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对协议驾校进行更新", null, user.getFid());

        return "redirect:/DrivingSys/select_driving/look.action?alertType=qd";
    }

    /**
     * @Author lcx
     * @Date 2017/11/2 0002 下午 4:28
     * 对财务基础资料删除
     */
    @RequestMapping(value = "delete_driving")
    public String deleteDriving(HttpServletRequest request, String[] deleteId) throws Exception {

        User user = (User) request.getSession().getAttribute("user");
        /*删除协议驾校 子类也删除*/
        drivingService.deleteByDrivingKey(deleteId);

        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对协议驾校进行删除", null, user.getFid());

        return "redirect:/DrivingSys/select_driving/look.action";
    }

    @RequestMapping("/queryDrivingXls")
    public String queryDrivingXls(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(defaultValue = "", value = "name") String name) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");

        //头部数据
        List<Object> data = new ArrayList<>();
        String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后创建时间", "创建者", "修改人"
        };
        //长度
        Integer length[] = {8500, 4000, 6000, 6000, 3000, 3000, 3000, 3000};

        List<Driving> drivingList = drivingService.selectDriving(user.getFcompanyid(), name);


        for (Driving driving : drivingList) {
            data.add(driving.getFnumber());
            data.add(driving.getFnameL2());
            data.add(driving.getFsimplename());
            data.add(driving.getFdescriptionL2());
            data.add(driving.getFcreatetime());
            data.add(driving.getFlastupdatetime());
            data.add(driving.getCreateName());
            data.add(driving.getUpdateName());
        }

        try {
            //导出Excel
            CommonExport.importExcel(response, "协议驾校" + TimeFormat.getNowTime2() + ".xls", "协议驾校", data, head, length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/DrivingSys/select_driving/look.action?state=first";
    }
}
